package sy.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.Customer;
import sy.service.CustomerServiceI;

@Controller
@RequestMapping("/customerController")
public class CustomerController {

	private CustomerServiceI customerService;

	public CustomerServiceI getCustomerService() {
		return customerService;
	}

	@Autowired
	public void setCustomerService(CustomerServiceI customerService) {
		this.customerService = customerService;
	}

	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	@RequestMapping("/datagrid")
	@ResponseBody
	public DataGrid datagrid(Customer customer) {
		DataGrid d = customerService.datagrid(customer);
		return d;
	}

	@RequestMapping("/remove")
	@ResponseBody
	public Json remove(Customer customer) {
		customerService.remove(customer.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("邀请成功！");
		return j;
	}

	@RequestMapping("/add")
	@ResponseBody
	public Json add(Customer customer) {
		System.out.println("add");
		Json j = new Json();
		try {
			Customer c = customerService.save(customer);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(c);
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		return j;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Customer customer) {
		Json j = new Json();
		try {
			Customer c = customerService.edit(customer);
			j.setSuccess(true);
			j.setMsg("修改成功！");
			j.setObj(c);
		} catch (Exception e) {
			j.setMsg("修改失败！" + e.getMessage());
		}
		System.out.println(j);
		return j;
	}
}
